# -*- coding: utf-8 -*-
# @Time    : 2019/10/17 9:55
# @Author  : LiHaowen
# @Email   : lihaowen2017@163.com
# @File    : dict_subclass.py
# 不推荐直接继承dict 和list
# 原因，重写某些方法时，由于底层是c实现的会导致某些方法失效
# 可以继承 UserDict


# class Mydict(dict):
#     def __setitem__(self, key, value):
#         super(Mydict, self).__setitem__(key, value*2)
#
#
# my_dict = Mydict(one=1)  # 重写方法失效
# print(my_dict)
# my_dict["one"] = 1
# print(my_dict)
# from collections import UserDict
#
#
# class NewMydict(UserDict):
#     def __setitem__(self, key, value):
#         super(NewMydict, self).__setitem__(key, value*2)
#
# new_mydict = NewMydict(one=1)
# print(new_mydict)

from collections import defaultdict

mydict = defaultdict(dict)
myval = mydict["b1"]  # 当找不到相应key时，会进入defaultdict的missing方法，实现模拟的defaultdict实现设定默认值
print(myval)